package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.MyUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 提现
 */
public class WithdrawDepositActivity extends BaseActivity implements View.OnClickListener, TextWatcher {
    private static final String TAG = "WithdrawDepositActivity";
    private static final int PAY_PASSWORK = 444;
    TextView title;
    private final int SELECT_BANK = 813;
    private TextView bank_user_name_tv;
    private TextView bank_mame_tv;
    private TextView bank_card_number_tv;
    private TextView my_assets;//总金额
    private TextView withdrawals_consume;//提现费用
    private TextView get_money;//到账金额
    private TextView time_tv;//到账时间

    private String bank_user_name;
    private String bank_mame;
    private String bank_card_number;
    private EditText money_ed;//提现金额
    private EditText passWork_ed;//交易密码
    private String money;
    private View bank_right_img;
    private View ts;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_deposit);
        initTirle();

        initView();



        initClick();
        if (id == null || "".equals(id)){
            bank_right_img.setVisibility(View.VISIBLE);
            ts.setVisibility(View.VISIBLE);
        }else {
            bank_right_img.setVisibility(View.GONE);
            ts.setVisibility(View.GONE);
        }
        time_tv.setText(time);
        get_money.setText("0.00元");
        withdrawals_consume.setText("0.00元");

        my_assets.setText(MyUtils.setColotSpannableString(MyApplication.getInstance().userBean.getSum(),0xffff3c3d));
        money_ed.addTextChangedListener(this);
    }

    private void initClick() {
        findViewById(R.id.select_bank_card).setOnClickListener(this);
        findViewById(R.id.btn).setOnClickListener(this);
    }

    private void initView() {
        bank_right_img = findViewById(R.id.bank_right_img);
        ts = findViewById(R.id.ts);
        bank_user_name_tv = (TextView) findViewById(R.id.bank_user_name);
        bank_mame_tv = (TextView) findViewById(R.id.bank_mame);
        bank_card_number_tv = (TextView) findViewById(R.id.bank_card_number);
        my_assets = (TextView) findViewById(R.id.my_assets);
        withdrawals_consume = (TextView) findViewById(R.id.withdrawals_consume);
        get_money = (TextView) findViewById(R.id.get_money);
        time_tv = (TextView) findViewById(R.id.time);

        money_ed = (EditText) findViewById(R.id.money_ed);
        passWork_ed = (EditText) findViewById(R.id.passWork);
    }

    private void initTirle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("提现");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        title.setFocusable(true);
        title.setFocusableInTouchMode(true);
        title.requestFocus();
    }

    private String passWork;
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.select_bank_card://选择银行卡

                //startActivity(new Intent(this,BankCardActivity.class).putExtra("type","选择银行卡"));
                startActivityForResult(new Intent(WithdrawDepositActivity.this,BankCardActivity.class).putExtra("type","选择银行卡"),SELECT_BANK);
                break;
            case R.id.btn:
                passWork = passWork_ed.getText().toString();
                if (id == null || "".equals(id)){
                    MyToast("请选择绑定银行卡");
                }else if (money == null || "".equals(money) || Double.parseDouble(money) == 0){
                    MyToast("请输入提取金额");
                }/*else if (passWork == null || "".equals(passWork)){
                    MyToast("请输入支付密码");
                }*/else {
                    Intent intent = new Intent(WithdrawDepositActivity.this, InPutPayPassWordActivity.class);
                    intent.putExtra("type",2);
                    startActivityForResult(intent,PAY_PASSWORK);
                    overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);

                }

                break;
        }
    }

    private void cash(String passWork) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        map.put("bankId",id);
        map.put("money",money);
        map.put("payPass",passWork);
        showDialog();
        XUtil.Post(ConfigUtil.CASH,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+result );
                try {
                    JSONObject json = new JSONObject(result);
                    String re = json.optString("result");
                    String info = json.optString("info");
                    MyToast(info);
                    if ("1".equals(re)){
                        MyApplication.getInstance().upUser();
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();

            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        money = money_ed.getText().toString();
        if (money.length() > 0){
            get_money.setText(money+"元");
        }else {
            //get_money.setText("1011元");
        }

        withdrawals_consume.setText("0.00元");
    }

    private String time = "";
    private String id;
    private String name = "";
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_BANK){
            if (resultCode == RESULT_OK){
                bank_mame = data.getStringExtra("bank_mame");
                bank_card_number = data.getStringExtra("bank_card_number");
                time = data.getStringExtra("time");
                id = data.getStringExtra("id");
                name = data.getStringExtra("name");
                bank_user_name_tv.setText(name);
                bank_mame_tv.setText(bank_mame);
                bank_card_number_tv.setText(bank_card_number);
                if (id == null || "".equals(id)){
                    bank_right_img.setVisibility(View.VISIBLE);
                    ts.setVisibility(View.VISIBLE);
                }else {
                    bank_right_img.setVisibility(View.GONE);
                    ts.setVisibility(View.GONE);
                }
                time_tv.setText(time);
            }
        }else if (requestCode == PAY_PASSWORK){
            if (resultCode == RESULT_OK){
                String pay_passwork = data.getStringExtra("passwork");
                cash(pay_passwork);
            }
        }
    }
}
